import camelot  
import pandas as pd  

def extract_cc_to_excel(pdf_path='', excel_path=''):  
    
    # 使用 camelot 读取 PDF 中的表格  
    tables = camelot.read_pdf(pdf_path, pages='all', flavor='stream')  
    
    # 创建一个 Excel 写入器  
    with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:  
        for index, table in enumerate(tables):  
            # 将 camelot 的表格转换为 DataFrame  
            df = table.df  
            # 为每个表格添加一个工作表  
            df.to_excel(writer, sheet_name=f'Sheet{index+1}', index=False)     
    
    # 使用 camelot 读取 PDF 中的表格  
    tables = camelot.read_pdf(pdf_path, pages='all', flavor='stream')
    # 创建一个 Excel 写入器  
    with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:  
        for index, table in enumerate(tables):  
            # 将 camelot 的表格转换为 DataFrame
            
            print(f'{index+1}') 
            df = table.df  
            # 为每个表格添加一个工作表  
            df.to_excel(writer, sheet_name=f'Sheet{index+1}', index=False)
    
    print("表格已成功保存到 Excel 文件。")

if __name__ == '__main__':
    pdf_path = f'CHS-DRG12\\CHS-DRG1.2版_1282-1480.pdf'
    excel_path = f'CHS-DRG12\\CHS-DRG1.2_cc.xlsx'
    extract_cc_to_excel(pdf_path, excel_path)